package com.rc.saas.mini.controller.tenant;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.rc.saas.base.MinipBaseController;
import com.rc.saas.mini.common.base.DataTResponse;
import com.rc.saas.mini.common.constant.SessionConstants;
import com.rc.saas.mini.minip.vo.LoginInfoVo;
import com.rc.saas.mini.model.member.MemberInfo;
import com.rc.saas.mini.model.tenant.TenantNotice;
import com.rc.saas.mini.service.member.MemberInfoService;
import com.rc.saas.mini.service.tenant.TenantNoticeService;
import com.rc.saas.mini.vo.base.RcPageVo;
import com.rc.saas.mini.vo.tenant.TenantNoticeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author
 * @Date: 2022-06-07
 */

@Api(tags = "25.运营商发送公告或通知")
@Controller
@RequestMapping("/tenantNotice")
public class TenantNoticeController extends MinipBaseController {

    private static Logger logger = LogManager.getLogger(TenantNoticeController.class);

    @Autowired
    private TenantNoticeService tenantNoticeService;
    @Autowired
    private MemberInfoService memberInfoService;


    /**
     * 查询
     */
    @ApiOperation(value = "查询单个 运营商发送公告或通知", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/open/selectByNoticeId", method = RequestMethod.POST)
    @ResponseBody
    private DataTResponse<TenantNotice> selectByNoticeId(HttpServletRequest request, Long noticeId) {
        String tenantCode = (String) request.getSession().getAttribute(SessionConstants.SESSION_TENANT);
        if (tenantCode == null || StringUtils.isBlank(tenantCode)) {
            logger.error("找不到对应运营商信息:{}", tenantCode);
            return DataTResponse.FAIL("找不到对应运营商信息");
        }
        LoginInfoVo loginInfo = readLoginInfo(request);

        if (noticeId != null) {
            TenantNotice tenantNotice = tenantNoticeService.selectByPrimaryKey(noticeId);

            return DataTResponse.SUCCESS_DATA(tenantNotice);
        }else{
            return DataTResponse.FAIL("noticeId参数为空");
        }


    }

    ///**
    // * 查询列表
    // */
    //@ApiOperation("查询列表 运营商发送公告或通知")
    //@RequestMapping(value = "/listByTenantNotice", method = RequestMethod.POST)
    //@ResponseBody
    //private DataTResponse listByTenantNotice(HttpServletRequest request, TenantNotice tenantNotice) {
    //    List<TenantNotice> list = tenantNoticeMapper.listByCondition(tenantNotice);
    //    return DataTResponse.SUCCESS_DATA(list);
    //}

    /**
     * 查询列表分页
     */
    @ApiOperation(value = "查询列表分页 运营商发送公告或通知", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/open/listPageByTenantNotice", method = RequestMethod.POST)
    @ResponseBody
    private DataTResponse<PageInfo<TenantNoticeVo>> listPageByTenantNotice(HttpServletRequest request, RcPageVo rcPageVo) {
        //Page
        PageHelper.startPage(rcPageVo.getPageNo(), rcPageVo.getPageSize());
        TenantNoticeVo tenantNotice = new TenantNoticeVo();
        tenantNotice.setStatus("1");
        LoginInfoVo loginInfo = readLoginInfo(request);
        if (loginInfo != null && loginInfo.getMemberId() != null) {
            tenantNotice.setMemberId(loginInfo.getMemberId());
        }

        List<TenantNoticeVo> list = tenantNoticeService.listByConditionWithMemberId(tenantNotice);
        if (tenantNotice.getMemberId() != null) {
            MemberInfo memberInfo = memberInfoService.selectByPrimaryKey(tenantNotice.getMemberId());
            list.forEach(data -> {
                if (memberInfo.getLastPlatformMsgTime()!=null&&data.getCreatedTime().compareTo(memberInfo.getLastPlatformMsgTime())<0) {
                    data.setIsRead("1");
                }
            });
            if(memberInfo!=null){
                memberInfo.setLastPlatformMsgTime(new Date());
                memberInfo.setLastUpdatedTime(new Date());
                memberInfoService.updateByPrimaryKey(memberInfo);
            }

        }

        PageInfo<TenantNoticeVo> pageInfo = new PageInfo<>(list);
        return DataTResponse.SUCCESS_DATA(pageInfo);
    }

    @ApiOperation(value = "查询首条 运营商发送公告或通知", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/open/selectFirstByTenantNotice", method = RequestMethod.POST)
    @ResponseBody
    private DataTResponse<TenantNoticeVo> selectFirstByTenantNotice(HttpServletRequest request) {
        //Page
        PageHelper.startPage(1, 1);
        TenantNoticeVo tenantNotice = new TenantNoticeVo();
        tenantNotice.setStatus("1");
        LoginInfoVo loginInfo = readLoginInfo(request);
        if (loginInfo != null && loginInfo.getMemberId() != null) {
            tenantNotice.setMemberId(loginInfo.getMemberId());
        }
        List<TenantNoticeVo> list = tenantNoticeService.listByConditionWithMemberId(tenantNotice);
        PageInfo<TenantNoticeVo> pageInfo = new PageInfo<>(list);
        return DataTResponse.SUCCESS_DATA(list==null?null:list.get(0));
    }
}
